package com.example.shixun.service.impl;

import com.example.shixun.dao.SchoolDao;
import com.example.shixun.pojo.School;
import com.example.shixun.pojo.Student;
import com.example.shixun.service.SchoolService;
import com.example.shixun.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author XDW
 * @Version 1.0
 * @date 2023/6/21 12:31
 */
@Service
public class SchoolServiceImpl implements SchoolService {
    @Resource
    private SchoolDao schoolDao;

    @Override
    public int saveSchool(School school) {
        return schoolDao.saveSchool(school);
    }

    @Override
    public int deleteSchool(int id) {
        return schoolDao.deleteSchoolById(id);
    }

    @Override
    public int deleteSchool(String name) {
        return schoolDao.deleteSchoolByName(name);
    }

    @Override
    public School select(int id) {
        return schoolDao.selectSchoolById(id);
    }

    @Override
    public School select(String name) {
        return schoolDao.selectSchoolByName(name);
    }

    @Override
    public PageInfoUtil<School> selectAll(PageInfoUtil<School> pageInfoUtil) {
        //该语句一定要在执行sql 语句之前，参数第一个参数代表当前第几页，第二个参数代表每页的数量
        PageHelper.startPage(pageInfoUtil.getPageNum(),pageInfoUtil.getPageSize());
        //System.out.println(pageInfoUtil.getPageSize());
        List<School> lists=schoolDao.selectSchoolAll(pageInfoUtil.getParams());

        PageInfo<School> pageInfo=  new PageInfo(lists);
        System.out.println(pageInfo);
        pageInfoUtil.setPageNum(pageInfo.getPageNum());
        pageInfoUtil.setPageSize(pageInfo.getPageSize());
        pageInfoUtil.setPageTotal(pageInfo.getPages());
        pageInfoUtil.setTotal(pageInfo.getTotal());
        pageInfoUtil.setList(lists);
        return pageInfoUtil;

    }

    @Override
    public Integer cancelDelete(Integer id) {
        return schoolDao.cancelDelete(id);
    }

    @Override
    public int updateSchool(School school) {
        return schoolDao.updateSchool(school);
    }
}
